//
//  statuses.swift
//  DSWeibo
//
//  Created by J on 16/4/17.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit


class Statuses: NSObject {

     /// 微博创建时间
    var created_at: String?
    /// 微博创建时间
    var id: Int = 0
    /// 微博ID
    var text: String?
    /// 微博来源
    var source: String?
    
    /// 配图数组
    var pic_urls: [[String:AnyObject]]?
    
    
    ///  加载微博数据
    class func loadStatuses(finished:(models:[Statuses]?, error : NSError?)->()){
        let path = "2/statuses/friends_timeline.json"
        let params = ["access_token":UserAccount.loadAcount()!.access_token!]
        NetWorkTools.shareNetWorkTools().GET(path, parameters: params, success: { (task:NSURLSessionDataTask, result:AnyObject?) in
           //1.取出status key对应数组(存储都是字典)
          let models = dict2Model(result!["statuses"]! as! [[String:AnyObject]])
            finished(models: models, error: nil)
        
            
            //2.遍历数组,将字典转换为模型
            
            
            
            
        }) { (task:NSURLSessionDataTask?, error:NSError) in
            print(error)
            finished(models: nil, error: error)

            
        }
        
    }
    
    
    
    class func dict2Model(list:[[String:AnyObject]])-> [Statuses]{
        var models = [Statuses]()
        for dic in list {
            models.append(Statuses(dic: dic))
        }
        return models
    }
    
    
    //字典转模型
    init(dic: [String: AnyObject]) {
        
        super.init()
            setValuesForKeysWithDictionary(dic)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        print("\(key):\(value)" )
    }
    
    
    //打印当前模型
    var properties = ["created_at","id","text","source","pic_urls"]
    
    override var description: String {
        let dict = dictionaryWithValuesForKeys(properties)
        return "\(dict)"
    }
}
